package com.atguigu.srb.core.controller.api;


import com.atguigu.common.result.Result;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.service.BorrowInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 借款信息表 前端控制器
 * </p>
 *
 * @author ZLHao
 * @since 2021-07-12
 */
@Api(tags = "借款申请")
@Slf4j
@RestController
@RequestMapping("/api/core/borrowInfo")
public class BorrowInfoController {

    @Autowired
    private BorrowInfoService borrowInfoService;


    @ApiOperation("获取借款审核状态")
    @GetMapping("auth/getBorrowInfoStatus")
    public Result getBorrowInfoStatus(HttpServletRequest request){
        // 获取当前用户  id
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        Integer status =  borrowInfoService.getBorrowInfoStatus(userId);
        return  Result.ok().data("borrowInfoStatus",status);

    }

    @ApiOperation("获取借款额度")
    @GetMapping("/auth/getBorrowAmount")
    public Result  getBorrowAmount(HttpServletRequest request){
        // 获取当前用户  id
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        Long borrowAmount =  borrowInfoService.getBorrowAmount(userId);
        return  Result.ok().data("borrowAmount",borrowAmount);
    }



    @ApiOperation("保存借款信息")
    @PostMapping("/auth/save")
    public Result save(@RequestBody BorrowInfo borrowInfo,HttpServletRequest request){

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        borrowInfoService.save(borrowInfo,userId);
        return Result.ok().message("提交成功");
    }

}

